package com.ygqh.baby.shiro.session;

import java.io.Serializable;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.SessionFactory;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.web.util.WebUtils;

public class SimpleSessionFactory implements SessionFactory {

	@Override
	public Session createSession(SessionContext initData) {
		if (initData != null) {
			String host = initData.getHost();
			if (host != null) {
				SimpleSession session = new SimpleSession(host);
				session.setId(getSessionId(initData));
				return session;
			}
		}
		return new SimpleSession();
	}

	public Serializable getSessionId(SessionContext key) {
		if(WebUtils.isWeb(key)) {
			ServletRequest request = WebUtils.getRequest(key);
			ServletResponse response = WebUtils.getResponse(key);
			return ShiroSessionUtil.getSessionId(request, response);
		}
		return null;
	}

}
